//
//  ViewController.m
//  06-超级猜图
//
//  Created by apple on 15/7/23.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#define kuan 32
#define juli 10
#define gao 32
#define gaojuli 5

#define hang 7

#import "ViewController.h"
#import "LMQuestion.h"
@interface ViewController ()<UIAlertViewDelegate>
/**中间的图片*/
@property (strong, nonatomic) IBOutlet UIButton *viewImage;

/**遮罩效果*/
@property (strong, nonatomic) IBOutlet UIButton *cover;


@property (strong, nonatomic) IBOutlet NSArray *quersion;
/**
 *  1/10属性
 */
@property (weak, nonatomic) IBOutlet UILabel *numlabel;

/**
 *  标题名称
 */
@property (weak, nonatomic) IBOutlet UILabel *titlelabel;
/**
 *  下一个按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *nextbutton;


/**
 *  答案UIView
 */
@property (weak, nonatomic) IBOutlet UIView *answerview;
/**
 *  可选答案
 */
@property (weak, nonatomic) IBOutlet UIView *optionsview;
@property (assign,nonatomic)  int index;
/**
 *
 *积分按钮
 */
@property (strong, nonatomic) IBOutlet UIButton *jifenbtn;
/**
 *
 *提示按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *tishibtn;

/**
 *
 * 提示的检索
 */
@property (assign,nonatomic)  int tishiindex;

@end

@implementation ViewController

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (void)viewDidLoad
{
    
    self.index = -1;
    [self nextImage];
//    1.加载数据
//    2.lable 加载数据
//    3.图片数据加载
    
    
}




- (NSArray *)quersion
{
    if(_quersion==nil)
    {
        _quersion = [LMQuestion questions];
    }
    return _quersion;
}


- (UIButton *) cover
{
    if(_cover ==nil)
    {
    _cover =[[UIButton alloc] initWithFrame:self.view.bounds];
    _cover.backgroundColor = [UIColor blackColor];
    _cover.alpha = 0.5;
    [self.view addSubview:_cover];
        
        [_viewImage addTarget:self action:@selector(bigImage) forControlEvents:UIControlEventTouchUpInside];
        [_cover addTarget:self action:@selector(bigImage) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cover;
}


- (IBAction)bigImage {
    if(self.cover.alpha ==0.0)
    {
    
    [self.view bringSubviewToFront:self.viewImage];
    
   
    
    [UIView animateWithDuration:1 animations:^{
        self.cover.alpha = 0.5;
         self.viewImage.frame = CGRectMake(0, self.viewImage.frame.size.height, self.view.frame.size.width, self.view.frame.size.height*0.5);
    } ];
    }
    else
    {
        [UIView animateWithDuration:1 animations:^{
            self.viewImage.frame = CGRectMake(85, 94, 150, 150);
        } completion:^(BOOL finished){
            self.cover.alpha = 0.0;
        } ];
    }
}

- (IBAction)nextImage {
    self.tishiindex = 0;
     self.index++;
    [self.tishibtn setEnabled:YES];
       self.optionsview.userInteractionEnabled = YES;
    //删除之前在ui答案里的button
//    for (UIButton *btn in self.answerview.subviews) {
//        [btn removeFromSuperview];
//    }
    [self.answerview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    
    if (self.index ==self.quersion.count) {
        UIAlertView *alerview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"恭喜完成" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alerview show];
        return;
    }
    
    //删除之前在ui选项里的button
    for (UIButton *btn in self.optionsview.subviews) {
        [btn removeFromSuperview];
    }
    LMQuestion *lmquestion = self.quersion[self.index];
   
    self.numlabel.text = [NSString stringWithFormat:@"%d/%d",self.index+1, self.quersion.count];
    
        self.titlelabel.text =lmquestion.title;
        [self.viewImage setImage:[UIImage imageNamed:lmquestion.icon] forState:UIControlStateNormal];
    
    if(self.index==(self.quersion.count-1))
    {
        [self.nextbutton setEnabled:NO];
    }
    
    /**
     *  设置答案
     */
     int answerlength = lmquestion.answer.length;

    int x = (self.view.frame.size.width - kuan*answerlength - juli*(answerlength-1))*0.5;
    
    int y = self.answerview.frame.size.height;
    
    
    
    for (int i = 0; i<answerlength; i++) {
        
        int xx = x +kuan*i + juli*i;;
        UIButton *answerbtn = [[UIButton alloc] initWithFrame:CGRectMake(xx, y-gao, kuan, gao)];
        
        [answerbtn setBackgroundImage:[UIImage imageNamed:@"btn_answer"] forState:UIControlStateNormal];
        [answerbtn setBackgroundImage:[UIImage imageNamed:@"btn_answer_highlighted"] forState:UIControlStateHighlighted];
        
        [answerbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
         [answerbtn addTarget:self action:@selector(answerClick:) forControlEvents:UIControlEventTouchUpInside];
       
        [self.answerview addSubview:answerbtn];
        
        
        
    }
    
    /**
     *  多多个按钮
     */
    
    
    for (int i = 0;i<lmquestion.options.count;i++) {
        int row = i/hang;
        
        int column = i%hang;
        
        CGFloat subViewX = juli + column*(juli + kuan);
        CGFloat subVIewY = 32+ row * (gaojuli + kuan);
        UIButton *optionsbtn = [[UIButton alloc] initWithFrame:CGRectMake(subViewX, subVIewY, kuan, gao)];
        
        
        optionsbtn.tag = i;
        [optionsbtn setTitle:lmquestion.options[i] forState:UIControlStateNormal];
        
        [optionsbtn setBackgroundImage:[UIImage imageNamed:@"btn_option"] forState:UIControlStateNormal];
        [optionsbtn setBackgroundImage:[UIImage imageNamed:@"btn_option_highlighted"] forState:UIControlStateHighlighted];
        
        
        [optionsbtn addTarget:self action:@selector(optionClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.optionsview addSubview:optionsbtn];

        
    }
    
}

- (void)optionClick:(UIButton *)sender
{
    
    sender.hidden = YES;
    for(UIButton *answerbtn in self.answerview.subviews)
    {

        if(answerbtn.currentTitle ==nil)
        {
            [answerbtn setTitle:sender.currentTitle forState:UIControlStateNormal];
            
            
            answerbtn.tag = sender.tag;
            break;
        }
        
        
    }
    BOOL isFull = YES;
    NSMutableString *inputanswter = [NSMutableString string];
    for(UIButton *answerbtn in self.answerview.subviews)
    {
        if (answerbtn.currentTitle ==nil) {
            isFull = NO;
            break;
        }
        [inputanswter appendString:answerbtn.currentTitle];
    }
    
    LMQuestion *lmquestion = self.quersion[self.index];
    if (isFull) {
        
        self.optionsview.userInteractionEnabled = NO;
        if([inputanswter isEqualToString: lmquestion.answer])
        {
            for (UIButton *btn in self.answerview.subviews) {
                [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
                
                int coin = [self.jifenbtn.currentTitle intValue];
                
                coin += 5000;
                
                [self.jifenbtn setTitle:[NSString stringWithFormat:@"%d",coin] forState:UIControlStateNormal];
            }
            
            [self performSelector:@selector(nextImage) withObject:nil afterDelay:3];
        }
        else
        {
            for (UIButton *btn in self.answerview.subviews) {
                [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            }
        }
    }
}

- (void)answerClick:(UIButton *)sender
{
    if (sender.currentTitle ==nil) {
        return ;
    }
    self.optionsview.userInteractionEnabled = YES;
   // sender.hidden = YES;

    for (UIButton *btn in self.answerview.subviews) {
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    
    for (UIButton *option in self.optionsview.subviews)
        {
            if(option.tag == sender.tag)
            {
                option.hidden = NO;
                break;
            }
        }
    [sender setTitle:nil forState:UIControlStateNormal];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%d",buttonIndex);
    
    if(buttonIndex ==1)
    {
        exit(0);
    }
}
- (IBAction)tishi {
      self.optionsview.userInteractionEnabled = YES;
    self.tishiindex++;
    LMQuestion *lmquestion = self.quersion[self.index];
    
    //1.清空anserview 中的数据
    //2.optionview中的数据还原
    //3.讲答案遍历取出1个放入anserview中，在安在取出一个
    
    if (self.tishiindex ==1) {
       
    
    for (UIButton *btn in self.answerview.subviews) {
        for (UIButton *option in self.optionsview.subviews)
        {
            if(option.tag == btn.tag)
            {
                option.hidden = NO;
                break;
            }
        }
        [btn setTitle:nil forState:UIControlStateNormal];

    }
        
    }
    
    NSString *first = [NSString stringWithFormat:@"%C",[lmquestion.answer characterAtIndex:self.tishiindex-1]];
    for (UIButton *option in self.optionsview.subviews)
    {
        if([first isEqualToString:option.currentTitle])
        {
            [self optionClick:option];
        }
    }
    if(self.tishiindex==lmquestion.answer.length)
    {
        [self.tishibtn setEnabled:NO];
    }
    int coin = [self.jifenbtn.currentTitle intValue];
    
    coin -= 5000;
    
    [self.jifenbtn setTitle:[NSString stringWithFormat:@"%d",coin] forState:UIControlStateNormal];

}

@end
